package com.hjq.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
// @Aspect
@Component
public class MyAspect {
    @Pointcut("execution(* com.hjq.service.impl.*.*(..))")
    public void pt(){}
    @Before("pt()")
    public void before(){
        log.info("前置通知");
    }
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp){
        log.info("环绕通知 目标方法执行之前");
        try {
            Object proceed = pjp.proceed();
            log.info("环绕通知 目标方法执行之前");
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
    // 无论目标方法是否异常，都会执行
    @After("pt()")
    public void after(){
        log.info("后置通知");
    }
    // 目标方法正常返回后才执行
    @AfterReturning("pt()")
    public void afterReturning(){
        log.info("返回后通知");
    }
    // 目标方法抛出异常后才执行
    @AfterThrowing("pt()")
    public void afterThrowing(){
        log.info("异常后通知");
    }
}
